﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
namespace PBCodeGen
{
    public static class LocalConfig
    {
        public static string ConfigPath="LocalConfig.xml";
        public static string ProtoFilePath;
        public static string CSharpHandlerPath;
        public static string CSharpDefinePath;
        public static string GoHandlerPath;
        public static string GoDefinePath;
        public static void LoadLocalConfig() 
        {
            //不存在返回
            if (!File.Exists(ConfigPath)) 
            {
                return;
            }

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(ConfigPath);

            XmlNode ProtoFileNode = xmldoc.SelectSingleNode("Paths/ProtoFilePath");
            if (ProtoFileNode != null) 
            {
                ProtoFilePath = ProtoFileNode.InnerText;
            }

            XmlNode CSharpHandlerNode = xmldoc.SelectSingleNode("Paths/CSharpHandlerPath");
            if (CSharpHandlerNode != null)
            {
                CSharpHandlerPath = CSharpHandlerNode.InnerText;
            }

            XmlNode CSharpDefineNode = xmldoc.SelectSingleNode("Paths/CSharpDefinePath");
            if (CSharpDefineNode != null)
            {
                CSharpDefinePath = CSharpDefineNode.InnerText;
            }

            XmlNode GoHandlerPathNode = xmldoc.SelectSingleNode("Paths/GoHandlerPath");
            if (GoHandlerPathNode != null) 
            {
                GoHandlerPath = GoHandlerPathNode.InnerText;
            }
            XmlNode GoDefinePathNode = xmldoc.SelectSingleNode("Paths/GoDefinePath");
            if (GoDefinePathNode != null) 
            {
                GoDefinePath = GoDefinePathNode.InnerText;
            }
        }


        public static void SaveLocalConfig(string protoFilePath, string CSharpHandlerPath, string CSharpDefinePath,string GoHandlerPath,string GoDefinePath) 
        { 
            XmlDocument xmldoc = new XmlDocument();
            XmlElement RootElem = xmldoc.CreateElement("Paths");
            xmldoc.AppendChild(RootElem);

            XmlElement protoFileNode = xmldoc.CreateElement("ProtoFilePath");
            protoFileNode.InnerText = protoFilePath;
            RootElem.AppendChild(protoFileNode);

            XmlElement CsharpHandlerNode = xmldoc.CreateElement("CSharpHandlerPath");
            CsharpHandlerNode.InnerText = CSharpHandlerPath;
            RootElem.AppendChild(CsharpHandlerNode);

            XmlElement CSharpDefineNode = xmldoc.CreateElement("CSharpDefinePath");
            CSharpDefineNode.InnerText = CSharpDefinePath;
            RootElem.AppendChild(CSharpDefineNode);

            XmlElement GoHandlerNode = xmldoc.CreateElement("GoHandlerPath");
            GoHandlerNode.InnerText = GoHandlerPath;
            RootElem.AppendChild(GoHandlerNode);

            XmlElement GoDefineNode = xmldoc.CreateElement("GoDefinePath");
            GoDefineNode.InnerText = GoDefinePath;
            RootElem.AppendChild(GoDefineNode);

            xmldoc.Save(ConfigPath); 
        }
    }
}
